/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.urchr.todoist.lib;

import java.util.Vector;

/**
 *
 * @author Kostya
 */
public class Project
{
    private int _rmsID;
    private String _parentID = "";

    private RichNameParser parser = new RichNameParser();
    private String id="", name="", color="", order="", indent="", cacheCount="";
    private boolean collapsed = false;

    private int indentValue = 0;
    private Vector subProjects = new Vector();
    private Vector tasks = new Vector();

    public int getRmsID() {
        return _rmsID;
    }
    public void setRmsID(int rmsID) {
        this._rmsID = rmsID;
    }

    public String getParentId() {
        return _parentID;
    }
    public void setParentId(String id) {
        this._parentID = id;
    }

    public String getCacheCount() {
        return cacheCount;
    }
    public void setCacheCount(String cacheCount) {
        this.cacheCount = cacheCount;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        parser.parseData(name);
    }

    public String getClearName(){
        return parser.getClearName();
    }
    
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public int getNIndent() {
        return indentValue;
    }
    public void setNIndent(int n) {
        indentValue = n;
        indent = String.valueOf(n);
    }
    public String getIndent() {
        return indent;
    }
    public void setIndent(String indent) {
        this.indent = indent;
        try {
            if (indent != null && !indent.equals("")) indentValue = Integer.parseInt(indent);
            else indentValue = 0;
        }
        catch (NumberFormatException ex) {
            indentValue = 0;
        }
    }

    public void addSubProject(Project p){
        subProjects.addElement(p);
        p.setParentId(id);
    }
    
    public int getSubProjectCount(){
        return subProjects.size();
    }
    
    public Project getSubProjectAt(int index){
        return (Project) subProjects.elementAt(index);
    }
    
    public void addTask(Task t){
        tasks.addElement(t);
    }
    
    public void removeTask(Task t){
        tasks.removeElement(t);
    }
    
    public int getTasksCount(){
        return tasks.size();
    }
    
    public Task getTaskAt(int index){
        return (Task) tasks.elementAt(index);
    }

    public String toString() {
        String ws = "";
        for (int i = 0; i < indentValue-1; i++) {
            ws += "  ";
        }
        return ws+(collapsed?"...":"")+parser.getClearName();
    }
    
    public void printProject(){
        String ws = "";
        for (int i = 0; i < indentValue-1; i++) {
            ws += " ";
        }
        System.out.println(ws+toString());
        for (int i = 0; i < getSubProjectCount(); i++) {
            getSubProjectAt(i).printProject();
        }
        for (int i = 0; i < getTasksCount(); i++) {
            getTaskAt(i).printTask(ws);
        }
    }

    public boolean isCollapsed() {
        return collapsed;
    }

    public void setCollapsed(boolean collapsed) {
        this.collapsed = collapsed;
    }
    
    public Task searchTask(String id){
        for (int i = 0; i < getTasksCount(); i++) {
            Task t = getTaskAt(i);
            if(t.getId().equals(id))
                return t;
            t = t.searchTask(id);
            if(t!=null)
                return t;
        }
        for (int i = 0; i < getSubProjectCount(); i++) {
            Project p = getSubProjectAt(i);
            Task t = p.searchTask(id);
            if(t!=null)
                return t;
        }

        return null;
    }
}
